package javafx.table.demo;

import java.util.List;

import javafx.scene.chart.AreaChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.util.StringConverter;

/**
 * @author hanxi
 * 
 */
public class ChartTableCell extends TableCell<Document, List<Double>> {
	NumberAxis xAxis;
	NumberAxis yAxis;
	AreaChart<Number, Number> chart;
	public ChartTableCell() {
		super();
		getStyleClass().add("table-chart-cell");
		xAxis = new NumberAxis();
		yAxis = new NumberAxis();
		chart = new AreaChart<Number, Number>(xAxis, yAxis);
		chart.getStyleClass().add("table-chart");
		chart.setLegendVisible(false);
		chart.setTitle(null);
		chart.setAnimated(false);

		xAxis.setLabel(null);
		xAxis.setTickMarkVisible(false);
		xAxis.setMinorTickVisible(false);
		xAxis.setTickLabelFormatter(new StringConverter<Number>() {
			@Override
			public String toString(Number t) {
				return "";
			}

			@Override
			public Number fromString(String string) {
				return 0;
			}
		});

		yAxis.setLabel(null);
		yAxis.setTickMarkVisible(false);
		yAxis.setMinorTickVisible(false);
		yAxis.setTickLabelFormatter(new StringConverter<Number>() {
			@Override
			public String toString(Number t) {
				return "";
			}

			@Override
			public Number fromString(String string) {
				return 0;
			}
		});
		chart.setMinSize(20, 35);
		chart.setPrefSize(150, 35);
		chart.setMaxSize(Double.MAX_VALUE, 35);
		setGraphic(chart);
		setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
	}

	@Override
	public void updateItem(List<Double> item, boolean empty) {
		super.updateItem(item, empty);
		if (empty) {
			setGraphic(null);
		} else {
			XYChart.Series<Number, Number> salesSeries = new XYChart.Series<Number, Number>();
			for (int i = 0; i < item.size(); i++) {
				salesSeries.getData().add(
						new XYChart.Data<Number, Number>(i, item.get(i)));
			}
			chart.getData().clear();
			chart.getData().add(salesSeries);
			setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
		}
	}
}
